package com.bangyu.monitor.core.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.logging.log4j.util.Strings;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Auther: zhangxue
 * @Date: 19/5/29 21:00
 * @Description:
 */
@Slf4j
public class FormatUtil {

    public static String percentFormat(Number number){
        if(number == null){
            return Strings.EMPTY;
        }
        NumberFormat format = NumberFormat.getPercentInstance();
        format.setMaximumFractionDigits(2);
        format.setMaximumIntegerDigits(3);
        format.setMinimumFractionDigits(2);
        format.setMinimumIntegerDigits(1);
        return format.format(number);
    }


    public static Integer parseDateStr2Int(String dateStr){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = dateFormat.parse(dateStr);
            return Integer.parseInt(DateFormatUtils.format(date,"yyyyMMdd"));
        } catch (ParseException e) {
            log.error("error",e);
        }
        return null;

    }
    public static Integer parseDate2Int(Date date){
        return Integer.parseInt(DateFormatUtils.format(date,"yyyyMMdd"));
    }

    public static String parseInt2DateStr(Integer dateInt){
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date = dateFormat.parse(dateInt.toString());
            return DateFormatUtils.format(date,"yyyy-MM-dd");
        } catch (ParseException e) {
            log.error("error",e);
        }
        return null;

    }

}
